<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="description" content="PragmaticScala">
<meta name="generator" content="Paradox, paradox-material-theme=0.5.1, mkdocs-material=3.0.3">

<meta name="lang:clipboard.copy" content="Copy to clipboard">
<meta name="lang:clipboard.copied" content="Copied to clipboard">
<meta name="lang:search.language" content="">
<meta name="lang:search.pipeline.stopwords" content="true">
<meta name="lang:search.pipeline.trimmer" content="true">
<meta name="lang:search.result.none" content="No matching documents">
<meta name="lang:search.result.one" content="1 matching document">
<meta name="lang:search.result.other" content="# matching documents">
<meta name="lang:search.tokenizer" content="[\s\-]+">


<meta name="description" content="PragmaticScala">
<link rel="shortcut icon" href="../assets/images/favicon.png">
<title>第8章——集合 · </title>
<link rel="stylesheet" href="../assets/stylesheets/application.451f80e5.css">
<link rel="stylesheet" href="../lib/material__tabs/dist/mdc.tabs.min.css">
<link rel="stylesheet" href="../lib/prettify/prettify.css">
<script src="../assets/javascripts/modernizr.1aa3b519.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,400i,700|Roboto+Mono">
<style>
body,input{font-family:"Roboto","Helvetica Neue",Helvetica,Arial,sans-serif}
code,kbd,pre{font-family:"Roboto Mono","Courier New",Courier,monospace}
</style>
<link rel="stylesheet" href="../assets/fonts/font-awesome.css">
<link rel="stylesheet" href="../assets/fonts/material-icons.css">
<link rel="stylesheet" href="../assets/stylesheets/paradox-material-theme.css">
</head>
<body
>
<input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
<input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
<label class="md-overlay" data-md-component="overlay" for="__drawer"></label>
<header class="md-header" data-md-component="header">
<nav class="md-header-nav md-grid">
<div class="md-flex">
<div class="md-flex__cell md-flex__cell--shrink">
<a href="../index.html" title="" class="md-header-nav__button md-logo">
<i class="md-icon">local_library</i>
</a>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<label class="md-icon md-icon--menu md-header-nav__button" for="__drawer"></label>
</div>
<div class="md-flex__cell md-flex__cell--stretch">
<div class="md-flex__ellipsis md-header-nav__title" data-md-component="title">
<span class="md-header-nav__topic">
</span>
<span class="md-header-nav__topic">
第8章——集合
</span>
</div>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<label class="md-icon md-icon--search md-header-nav__button" for="__search"></label>
<div class="md-search" data-md-component="search" role="dialog">
<label class="md-search__overlay" for="__search"></label>
<div class="md-search__inner" role="search">
<form class="md-search__form" name="search">
<input type="text" class="md-search__input" name="query" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="query" data-md-state="active">
<label class="md-icon md-search__icon" for="__search"></label>
<button type="reset" class="md-icon md-search__icon" data-md-component="reset" tabindex="-1">&#xE5CD;</button>
</form>
<div class="md-search__output">
<div class="md-search__scrollwrap" data-md-scrollfix>
<div class="md-search-result" data-md-component="result">
<div class="md-search-result__meta">
Type to start searching
</div>
<ol class="md-search-result__list"></ol>
</div>
</div>
</div>
</div>
</div>

</div>
</div>
</nav>
</header>

<div class="md-container">
<main class="md-main">
<div class="md-main__inner md-grid" data-md-component="container">
<div class="md-sidebar md-sidebar--primary" data-md-component="navigation">
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
<nav class="md-nav md-nav--primary" data-md-level="0" style="visibility: hidden">
<label class="md-nav__title md-nav__title--site" for="drawer">
<a href="../index.html" title="" class="md-nav__button md-logo">
<span class="md-nav__button md-logo">
<i class="md-icon">local_library</i>
</a>
<a href="../index.html" title="">
</a>
</label>
<ul>
  <li><a href="../contents.html" class="page">目录</a>
  <ul>
    <li><a href="../abstract.html" class="page">内容提要</a></li>
    <li><a href="../part1.html" class="page">第一部分 小试牛刀</a></li>
    <li><a href="../part2.html" class="page">第二部分 深入 Scala</a></li>
    <li><a href="../part3.html" class="page">第三部分 Scala 中的并发编程</a></li>
    <li><a href="../part4.html" class="page">第四部分 Scala 实战</a></li>
    <li><a href="../refs/references.html" class="page">参考书目</a></li>
    <li><a href="../extras.html" class="page">额外指引</a></li>
    <li><a href="../errata.html" class="page">勘误</a></li>
  </ul></li>
</ul>
<nav class="md-nav md-nav--secondary">
<label class="md-nav__title" for="__toc">Table of contents</label>
<ul>
  <li><a href="../chapter-08/index.html#第8章-集合" class="header">第8章——集合</a>
  <ul>
    <li><a href="../chapter-08/index.html#8-1-常见的-scala-集合" class="header">8.1 常见的 Scala 集合</a></li>
    <li><a href="../chapter-08/index.html#8-2-使用-set" class="header">8.2 使用 Set</a></li>
    <li><a href="../chapter-08/index.html#8-3-关联映射" class="header">8.3 关联映射</a></li>
    <li><a href="../chapter-08/index.html#8-4-不可变列表" class="header">8.4 不可变列表</a></li>
    <li><a href="../chapter-08/index.html#8-5-方法名约定" class="header">8.5 方法名约定</a></li>
    <li><a href="../chapter-08/index.html#8-6-for-表达式" class="header">8.6 for 表达式</a></li>
  </ul></li>
</ul>
</nav>

</nav>
<ul style="display: none">
<li class="md-nav__item md-version" id="project.version">
<label class="md-nav__link" for="__version">
<i class="md-icon" title="Version">label_outline</i> 1.0.0
</label>
</li>
</ul>
</div>
</div>
</div>
<div class="md-sidebar md-sidebar--secondary" data-md-component="toc">
<div class="md-sidebar__scrollwrap">
<div class="md-sidebar__inner">
<nav class="md-nav md-nav--secondary">
<label class="md-nav__title" for="__toc">Table of contents</label>
<ul>
  <li><a href="../chapter-08/index.html#第8章-集合" class="header">第8章——集合</a>
  <ul>
    <li><a href="../chapter-08/index.html#8-1-常见的-scala-集合" class="header">8.1 常见的 Scala 集合</a></li>
    <li><a href="../chapter-08/index.html#8-2-使用-set" class="header">8.2 使用 Set</a></li>
    <li><a href="../chapter-08/index.html#8-3-关联映射" class="header">8.3 关联映射</a></li>
    <li><a href="../chapter-08/index.html#8-4-不可变列表" class="header">8.4 不可变列表</a></li>
    <li><a href="../chapter-08/index.html#8-5-方法名约定" class="header">8.5 方法名约定</a></li>
    <li><a href="../chapter-08/index.html#8-6-for-表达式" class="header">8.6 for 表达式</a></li>
  </ul></li>
</ul>
</nav>

</div>
</div>
</div>
<div class="md-content">
<article class="md-content__inner md-typeset">
<div class="md-content__searchable">
<h1><a href="#第8章-集合" name="第8章-集合" class="anchor"><span class="anchor-link"></span></a>第8章——集合</h1>
<p>Scala 标准库包含了一组丰富的集合类，以及用于组合、遍历和提取元素的强大操作。在 创建 Scala 应用程序时，会经常用到这些集合。如果想要在使用 Scala 时更加具有生产力，彻 底地学习这些集合是很有必要的。</p>
<h2><a href="#8-1-常见的-scala-集合" name="8-1-常见的-scala-集合" class="anchor"><span class="anchor-link"></span></a>8.1 常见的 Scala 集合</h2>
<blockquote>
  <p>UsingCollections/UsingSet.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">val colors1 = Set(&quot;Blue&quot;, &quot;Green&quot;, &quot;Red&quot;)
println(s&quot;colors1: $colors1&quot;)

val colors2 = colors1 + &quot;Black&quot;
println(s&quot;colors2: $colors2&quot;)
println(s&quot;colors1: $colors1&quot;)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/UsingSet.scala#L21-L26">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">colors1: Set(Blue, Green, Red)
colors2: Set(Blue, Green, Red, Black)
colors1: Set(Blue, Green, Red)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/UsingSet.output#L2-L4">Full source at GitHub</a>
<pre class="prettyprint"><code class="language-output">scala&gt;  val colors = Set(&quot;Blue&quot;, &quot;Green&quot;, &quot;Red&quot;)
colors: scala.collection.immutable.Set[String] = Set(Blue, Green, Red)

scala&gt; colors.getClass
res0: Class[_ &lt;: scala.collection.immutable.Set[String]] = class 
scala.collection.immutable.Set$Set3

scala&gt; :quit</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/shoutput/RunREPL.output#L2-L9">Full source at GitHub</a>
<h2><a href="#8-2-使用-set" name="8-2-使用-set" class="anchor"><span class="anchor-link"></span></a>8.2 使用 Set</h2>
<blockquote>
  <p>UsingCollections/UsingSet.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">val feeds1 = Set(&quot;blog.toolshed.com&quot;, &quot;pragdave.me&quot;, &quot;blog.agiledeveloper.com&quot;)
val feeds2 = Set(&quot;blog.toolshed.com&quot;, &quot;martinfowler.com/bliki&quot;)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithSet.scala#L22-L23">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/UsingSet.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">val blogFeeds = feeds1 filter (_ contains &quot;blog&quot;)
println(s&quot;blog feeds: ${blogFeeds.mkString(&quot;, &quot;)}&quot;)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithSet.scala#L27-L28">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">blog feeds: blog.toolshed.com, blog.agiledeveloper.com</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/PlayingWithSet.output#L2">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/UsingSet.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">val mergedFeeds = feeds1 ++ feeds2
println(s&quot;# of merged feeds: ${mergedFeeds.size}&quot;)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithSet.scala#L32-L33">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output"># of merged feeds: 4</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/PlayingWithSet.output#L6">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/UsingSet.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">val commonFeeds = feeds1 &amp; feeds2
println(s&quot;common feeds: ${commonFeeds.mkString(&quot;, &quot;)}&quot;)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithSet.scala#L37-L38">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">common feeds: blog.toolshed.com</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/PlayingWithSet.output#L10">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/UsingSet.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">val urls = feeds1 map (&quot;http://&quot; + _)
println(s&quot;One url: ${urls.head}&quot;)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithSet.scala#L42-L43">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">One url: http://blog.toolshed.com</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/PlayingWithSet.output#L14">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/UsingSet.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">println(&quot;Refresh Feeds:&quot;)
feeds1 foreach { feed ⇒ println(s&quot;  Refreshing $feed...&quot;) }</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithSet.scala#L47-L48">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">Refresh Feeds:
  Refreshing blog.toolshed.com...
  Refreshing pragdave.me...
  Refreshing blog.agiledeveloper.com...</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/PlayingWithSet.output#L18-L21">Full source at GitHub</a>
<h2><a href="#8-3-关联映射" name="8-3-关联映射" class="anchor"><span class="anchor-link"></span></a>8.3 关联映射</h2>
<blockquote>
  <p>UsingCollections/PlayingWithMap.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">val feeds = Map(
  &quot;Andy Hunt&quot; -&gt; &quot;blog.toolshed.com&quot;,
  &quot;Dave Thomas&quot; -&gt; &quot;pragdave.me&quot;,
  &quot;NFJS&quot; -&gt; &quot;nofluffjuststuff.com/blog&quot;)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithMap.scala#L21-L24">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/PlayingWithMap.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">val filterNameStartWithD = feeds filterKeys (_ startsWith &quot;D&quot;)
println(s&quot;# of Filtered: ${filterNameStartWithD.size}&quot;)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithMap.scala#L28-L29">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output"># of Filtered: 1</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/PlayingWithMap.output#L2">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/PlayingWithMap.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">val filterNameStartWithDAndPragprogInFeed = feeds filter { element ⇒
  val (key, value) = element
  (key startsWith &quot;D&quot;) &amp;&amp; (value contains &quot;pragdave&quot;)
}
print(&quot;# of feeds with auth name D* and pragdave in URL: &quot;)
println(filterNameStartWithDAndPragprogInFeed.size)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithMap.scala#L33-L38">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output"># of feeds with auth name D* and pragprog in URL: 1</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/PlayingWithMap.output#L6">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/PlayingWithMap.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">println(s&quot;Get Andy&#39;s Feed: ${feeds.get(&quot;Andy Hunt&quot;)}&quot;)
println(s&quot;Get Bill&#39;s Feed: ${feeds.get(&quot;Bill Who&quot;)}&quot;)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithMap.scala#L42-L43">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">Get Andy&#39;s Feed: Some(blog.toolshed.com)
Get Bill&#39;s Feed: None</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/PlayingWithMap.output#L10-L11">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/PlayingWithMap.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">try {
  println(s&quot;Get Andy&#39;s Feed Using apply(): ${feeds(&quot;Andy Hunt&quot;)}&quot;)
  print(&quot;Get Bill&#39;s Feed: &quot;)
  println(feeds(&quot;Bill Who&quot;))
} catch {
  case _: java.util.NoSuchElementException ⇒ println(&quot;Not found&quot;)
}</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithMap.scala#L47-L53">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">Get Andy&#39;s Feed Using apply(): blog.toolshed.com
Get Bill&#39;s Feed: Not found</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/PlayingWithMap.output#L15-L16">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/PlayingWithMap.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">val newFeeds1 = feeds.updated(&quot;Venkat Subramaniam&quot;, &quot;blog.agiledeveloper.com&quot;)
println(&quot;Venkat&#39;s blog in original feeds: &quot; + feeds.get(&quot;Venkat Subramaniam&quot;))
println(&quot;Venkat&#39;s blog in new feed: &quot; + newFeeds1(&quot;Venkat Subramaniam&quot;))</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithMap.scala#L57-L59">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">Venkat&#39;s blog in original feeds: None
Venkat&#39;s blog in new feed: blog.agiledeveloper.com</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/PlayingWithMap.output#L20-L21">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/PlayingWithMap.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">val mutableFeeds = scala.collection.mutable.Map(
  &quot;Scala Book Forum&quot; -&gt; &quot;forums.pragprog.com/forums/87&quot;)
mutableFeeds(&quot;Groovy Book Forum&quot;) = &quot;forums.pragprog.com/forums/246&quot;
println(s&quot;Number of forums: ${mutableFeeds.size}&quot;)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithMap.scala#L63-L66">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">Number of forums: 2</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/PlayingWithMap.output#L25">Full source at GitHub</a>
<h2><a href="#8-4-不可变列表" name="8-4-不可变列表" class="anchor"><span class="anchor-link"></span></a>8.4 不可变列表</h2>
<blockquote>
  <p>UsingCollections/PlayingWithList.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">val feeds = List(&quot;blog.toolshed.com&quot;, &quot;pragdave.me&quot;, &quot;blog.agiledeveloper.com&quot;)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithList.scala#L22">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/PlayingWithList.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">println(s&quot;First feed: ${feeds.head}&quot;)
println(s&quot;Second feed: ${feeds(1)}&quot;)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithList.scala#L26-L27">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">First feed: blog.toolshed.com
Second feed: pragdave.me</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/PlayingWithList.output#L2-L3">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/PlayingWithList.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">val prefixedList = &quot;forums.pragprog.com/forums/87&quot; :: feeds
println(s&quot;First Feed In Prefixed: ${prefixedList.head}&quot;)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithList.scala#L31-L32">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">First Feed In Prefixed: forums.pragprog.com/forums/87</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/PlayingWithList.output#L7">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/PlayingWithList.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">val feedsWithForums =
  feeds ::: List(
    &quot;forums.pragprog.com/forums/87&quot;,
    &quot;forums.pragprog.com/forums/246&quot;)
println(s&quot;First feed in feeds with forum: ${feedsWithForums.head}&quot;)
println(s&quot;Last feed in feeds with forum: ${feedsWithForums.last}&quot;)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithList.scala#L36-L41">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">First feed in feeds with forum: blog.toolshed.com
Last feed in feeds with forum: forums.pragprog.com/forums/246</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/PlayingWithList.output#L11-L12">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/PlayingWithList.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">val appendedList = feeds ::: List(&quot;agilelearner.com&quot;)
println(s&quot;Last Feed In Appended: ${appendedList.last}&quot;)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithList.scala#L45-L46">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">Last Feed In Appended: agilelearner.com</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/PlayingWithList.output#L16">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/PlayingWithList.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">println(s&quot;Feeds with blog: ${feeds.filter(_ contains &quot;blog&quot;).mkString(&quot;, &quot;)}&quot;)
println(s&quot;All feeds have com: ${feeds.forall(_ contains &quot;com&quot;)}&quot;)
println(s&quot;All feeds have dave: ${feeds.forall(_ contains &quot;dave&quot;)}&quot;)
println(s&quot;Any feed has dave: ${feeds.exists(_ contains &quot;dave&quot;)}&quot;)
println(s&quot;Any feed has bill: ${feeds.exists(_ contains &quot;bill&quot;)}&quot;)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithList.scala#L50-L54">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">Feeds with blog: blog.toolshed.com, blog.agiledeveloper.com
All feeds have com: false
All feeds have dave: false
Any feed has dave: true
Any feed has bill: false</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/PlayingWithList.output#L20-L24">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/PlayingWithList.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">println(s&quot;Feed url lengths: ${feeds.map(_.length).mkString(&quot;, &quot;)}&quot;)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithList.scala#L58">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">Feed url lengths: 17, 11, 23</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/PlayingWithList.output#L28">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/PlayingWithList.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">val total = feeds.foldLeft(0) { (total, feed) ⇒ total + feed.length }
println(s&quot;Total length of feed urls: $total&quot;)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithList.scala#L62-L63">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">Total length of feed urls: 51</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/PlayingWithList.output#L32">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/PlayingWithList.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">val total2 = (0 /: feeds) { (total, feed) ⇒ total + feed.length }
println(s&quot;Total length of feed urls: $total2&quot;)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithList.scala#L67-L68">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">Total length of feed urls: 51</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/PlayingWithList.output#L36">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/PlayingWithList.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">val total3 = (0 /: feeds) { _ + _.length }
println(s&quot;Total length of feed urls: $total3&quot;)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PlayingWithList.scala#L72-L73">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">Total length of feed urls: 51</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/PlayingWithList.output#L40">Full source at GitHub</a>
<h2><a href="#8-5-方法名约定" name="8-5-方法名约定" class="anchor"><span class="anchor-link"></span></a>8.5 方法名约定</h2>
<blockquote>
  <p>UsingCollections/Colon.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">class Cow {
  def ^(moon: Moon): Unit = println(&quot;Cow jumped over the moon&quot;)
}
class Moon {
  def ^:(cow: Cow): Unit = println(&quot;This cow jumped over the moon too&quot;)
}</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/Colon.scala#L21-L26">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/Colon.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">val cow = new Cow
val moon = new Moon

cow ^ moon
cow ^: moon</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/Colon.scala#L30-L34">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">Cow jumped over the moon
This cow jumped over the moon too</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/Colon.output#L2-L3">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/Unary.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">class Sample {
  def unary_+(): Unit = println(&quot;Called unary +&quot;)
  def unary_-(): Unit = println(&quot;called unary -&quot;)
  def unary_!(): Unit = println(&quot;called unary !&quot;)
  def unary_~(): Unit = println(&quot;called unary ~&quot;)
}

val sample = new Sample
+sample
-sample
!sample
~sample</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/Unary.scala#L22-L33">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">Called unary +
called unary -
called unary !
called unary ~</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/Unary.output#L2-L5">Full source at GitHub</a>
<h2><a href="#8-6-for-表达式" name="8-6-for-表达式" class="anchor"><span class="anchor-link"></span></a>8.6 for 表达式</h2>
<blockquote>
  <p>UsingCollections/PowerOfFor.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">for (_ ← 1 to 3) { print(&quot;ho &quot;) }</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PowerOfFor.scala#L21">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/PowerOfFor.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">val result = for (i ← 1 to 10)
  yield i * 2</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PowerOfFor.scala#L25-L26">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/PowerOfFor.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">val result2 = (1 to 10).map(_ * 2)</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PowerOfFor.scala#L30">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/PowerOfFor.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">val doubleEven = for (i ← 1 to 10; if i % 2 == 0)
  yield i * 2</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PowerOfFor.scala#L34-L35">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/PowerOfFor.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">for {
  i ← 1 to 10
  if i % 2 == 0
} yield i * 2</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/PowerOfFor.scala#L39-L42">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/Friends.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">class Person(val firstName: String, val lastName: String)
object Person {
  def apply(firstName: String, lastName: String): Person =
    new Person(firstName, lastName)
}
val friends = List(Person(&quot;Brian&quot;, &quot;Sletten&quot;), Person(&quot;Neal&quot;, &quot;Ford&quot;),
  Person(&quot;Scott&quot;, &quot;Davis&quot;), Person(&quot;Stuart&quot;, &quot;Halloway&quot;))

val lastNames =
  for (friend ← friends; lastName = friend.lastName) yield lastName

println(lastNames.mkString(&quot;, &quot;))</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/Friends.scala#L21-L32">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">Sletten, Ford, Davis, Halloway</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/Friends.output#L2">Full source at GitHub</a>
<blockquote>
  <p>UsingCollections/MultipleLoop.scala</p>
</blockquote>
<pre class="prettyprint"><code class="language-scala">for (i ← 1 to 3; j ← 4 to 6) {
  print(s&quot;[$i,$j] &quot;)
}</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/MultipleLoop.scala#L19-L21">Full source at GitHub</a>
<p><em>运行结果</em></p>
<pre class="prettyprint"><code class="language-output">[1,4] [1,5] [1,6] [2,4] [2,5] [2,6] [3,4] [3,5] [3,6]</code></pre><a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/v1.0.0/src/main/scala/chapter8/output/MultipleLoop.output#L2">Full source at GitHub</a>
</div>
<div>
<a href="https://github.com/ReactivePlatform/Pragmatic-Scala.git/tree/master/src/paradox/chapter-08/index.md" title="Edit this page" class="md-source-file md-edit">
Edit this page
</a>
</div>
<div class="print-only">
<span class="md-source-file md-version">
1.0.0
</span>
</div>
</article>
</div>
</div>
</main>
<footer class="md-footer">
<div class="md-footer-nav">
<nav class="md-footer-nav__inner md-grid">
<a href="../chapter-07/index.html" title="第7章——特质" class="md-flex md-footer-nav__link md-footer-nav__link--prev" rel="prev">
<div class="md-flex__cell md-flex__cell--shrink">
<i class="md-icon md-icon--arrow-back md-footer-nav__button"></i>
</div>
<div class="md-flex__cell md-flex__cell--stretch md-footer-nav__title">
<span class="md-flex__ellipsis">
<span class="md-footer-nav__direction">
Previous
</span>
第7章——特质
</span>
</div>
</a>
<a href="../chapter-09/index.html" title="第9章——模式匹配和正则表达式" class="md-flex md-footer-nav__link md-footer-nav__link--next" rel="next">
<div class="md-flex__cell md-flex__cell--stretch md-footer-nav__title">
<span class="md-flex__ellipsis">
<span class="md-footer-nav__direction">
Next
</span>
第9章——模式匹配和正则表达式
</span>
</div>
<div class="md-flex__cell md-flex__cell--shrink">
<i class="md-icon md-icon--arrow-forward md-footer-nav__button"></i>
</div>
</a>
</nav>
</div>
<div class="md-footer-meta md-typeset">
<div class="md-footer-meta__inner md-grid">
<div class="md-footer-copyright">
Powered by
<a href="https://github.com/lightbend/paradox">Paradox</a>
and
<a href="https://jonas.github.io/paradox-material-theme/">Paradox Material Theme</a>

</div>
</div>
</div>
</footer>

</div>
<script src="../assets/javascripts/application.583bbe55.js"></script>
<script src="../assets/javascripts/paradox-material-theme.js"></script>
<script>app.initialize({version:"0.17",url:{base:"../."}})</script>
<script type="text/javascript" src="../lib/prettify/prettify.js"></script>
<script type="text/javascript" src="../lib/prettify/lang-scala.js"></script>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
window.prettyPrint && prettyPrint();
});
</script>
</body>
</html>
